//
//  SoundPlayerWrapper.mm
//  SoundEngine
//
//  Created by handcn on 09-3-13.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SoundPlayerWrapper.h"
#import "SoundPlayer.h"

@interface SoundPlayerWrapper(Private)
	CSoundPlayer *m_pPlayer;

#ifndef kPlayerUseThread
NSTimer			*m_pPlayerTimer;
-(void)PlayerTimer;
#endif

@end


@implementation SoundPlayerWrapper

#ifndef kPlayerUseThread
-(void)PlayerTimer
{
	if (m_pPlayer)
	{
		m_pPlayer->soundUpdate(0.0f);
	}
}
#endif	//	kPlayerUseThread

-(id)init
{
	if ( self = [super init] )
	{
		m_pPlayer = CSoundPlayer::GetInstance();
		
#ifndef kPlayerUseThread
		m_pPlayerTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f/30.0f 
														  target:self 
														selector:@selector(PlayerTimer) 
														userInfo:nil 
														 repeats:YES];
#endif	//	kPlayerUseThread
	}
	
	return self;
}

-(void)dealloc
{
#ifndef kPlayerUseThread
	if (m_pPlayerTimer)
	{
		[m_pPlayerTimer invalidate];
		m_pPlayerTimer = nil;
	}
#endif
	
	if ( m_pPlayer )
	{
		m_pPlayer->Release();
	}
	[super dealloc];
}

//	添加一首音乐
-(bool)Add:(NSString*)szFileName
{
	return [self Add: szFileName bQueue:false];
}

-(bool)Add:(NSString*)szFileName bQueue:(bool)bQueue
{
	if ( !szFileName ) return false;
	
	if ( m_pPlayer )
	{
		return m_pPlayer->soundAdd([szFileName UTF8String], bQueue);
	}
	return false;
}

//	移除一首音乐
-(bool)Remove:(NSString*)szFileName
{
	if ( !szFileName ) return false;
	
	if ( m_pPlayer )
	{
		return m_pPlayer->soundRemove([szFileName UTF8String]);
	}
	return false;
}

//	清空音乐列表，即移除所有音乐
-(bool)Clear
{
	if ( m_pPlayer )
	{
		return m_pPlayer->soundClear();
	}
	return false;
}

//	播放一首音乐
-(bool)Play:(NSString*)szFileName Looping:(bool)bLooping
{
	if ( !szFileName ) return false;
	
	if ( m_pPlayer )
	{
		return m_pPlayer->soundPlay([szFileName UTF8String], bLooping);
	}
	return false;
}

//	停止播放一首音乐
-(bool)Stop:(NSString*)szFileName
{
	if ( !szFileName ) return false;
	
	if ( m_pPlayer )
	{
		return m_pPlayer->soundStop([szFileName UTF8String]);
	}
	return false;
}

//	暂停播放一首音乐
-(bool)Pause:(NSString*)szFileName
{
	if ( !szFileName ) return false;
	
	if ( m_pPlayer )
	{
		return m_pPlayer->soundPause([szFileName UTF8String]);
	}
	return false;
}

//	停止所有播放
-(bool)StopAll
{
	if ( m_pPlayer )
	{
		return m_pPlayer->soundStopAll();
	}
	return false;
}

//	获取一首音乐的播放状态
-(int)State:(NSString*)szFileName
{
	int state = CSoundPlayer::SOUND_STATE_UNKNOWN;
	if ( !szFileName ) return state;
	
	if ( m_pPlayer )
	{
		m_pPlayer->soundState([szFileName UTF8String], state);
	}
	return state;		
}

//	是否正在播放
-(bool)IsPlaying:(NSString*)szFileName
{
	int state = CSoundPlayer::SOUND_STATE_UNKNOWN;
	if (!szFileName) return false;
	
	if ( m_pPlayer )
	{
		m_pPlayer->soundState([szFileName UTF8String], state);
	}
	return (CSoundPlayer::SOUND_STATE_PLAYING == state);
}

//	设置音量，分两种情况：
//	1.如果szFileName == nil，则设置听者的音量，影响所有的声音源，音量范围是0.0f - 4.0f
//	2.如果szFileName != nil，则设置单个声音源的音量，音量范围是0.0f - 1.0f
-(bool)SetVolume:(NSString*)szFileName Volume:(float)fVolume
{
	if ( m_pPlayer )
	{
		if (szFileName)
		{
			return m_pPlayer->soundVolume([szFileName UTF8String], fVolume);
		}
		else
		{
			return m_pPlayer->soundVolume(NULL, fVolume);
		}
	}
	return false;
}

@end
